<#include "/macro.include"/>
<#include "/custom.include">
<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
package com.github.wxiaoqi.security.checkout.rest;

import com.github.wxiaoqi.security.checkout.common.utils.util.PageList;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.wxiaoqi.security.checkout.model.${className}Model;
import com.github.wxiaoqi.security.checkout.service.${className}Service;
import java.util.HashMap;
import java.util.Map;

/**
* 类名: ${className}Controller
* @author wjt
*/
@RestController
@RequestMapping("${classNameLower}")
public class ${className}Controller {
    private final static Logger logger = LoggerFactory.getLogger(${className}Controller.class);

    @Autowired
    private ${className}Service ${classNameLower}Service;

    @ApiOperation("保存")
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Object> save(@RequestBody ${className}Model ${classNameLower}Model){
        logger.info("${className}Controller.save()");
        ${classNameLower}Service.save(${classNameLower}Model);
        return new ObjectRestResponse<Object>().rel(true);
    }

    @ApiOperation("根据ID获取记录信息")
    @RequestMapping(value = "findById",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<${className}Model> findById(@RequestParam(value = "<#list table.columns as column><#if column.pk>${column.columnNameLower}</#if></#list>") <#list table.columns as column><#if column.pk><#if column.javaType == "java.lang.Long">Long</#if><#elseif column.javaType == "java.lang.Integer">Integer<#else></#if></#list> <#list table.columns as column><#if column.pk>${column.columnNameLower}</#if></#list>){
        logger.info("${className}Controller.findById()");
        ${className}Model model = ${classNameLower}Service.findById(<#list table.columns as column><#if column.pk>${column.columnNameLower}</#if></#list>);
        return new ObjectRestResponse<${className}Model>().rel(true).data(model);
    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "findPageList",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<Object> findPageList(${className}Model ${classNameLower}Model, @RequestParam(value = "pageIndex", required = false) Integer pageIndex, @RequestParam(value = "pageSize", required = false) Integer pageSize){
        logger.info("${className}Controller.findPageList()");
        if (pageIndex == null) pageIndex = 0;
        if (pageSize == null) pageSize = 10;
        PageList<${className}Model> pageList = ${classNameLower}Service.pageListByParams(${classNameLower}Model, pageIndex, pageSize);
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("listData", pageList);
        resultData.put("paginator", pageList.getPaginator());
        return new ObjectRestResponse<Object>().rel(true).data(resultData);
    }

    @ApiOperation("删除")
    @RequestMapping(value = "del/{<#list table.columns as column><#if column.pk>${column.columnNameLower}</#if></#list>}",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Object> del(@PathVariable <#list table.columns as column><#if column.pk><#if column.javaType == "java.lang.Long">Long</#if><#elseif column.javaType == "java.lang.Integer">Integer<#else></#if></#list> <#list table.columns as column><#if column.pk>${column.columnNameLower}</#if></#list>){
        logger.info("${className}Controller.del()");
        int i = ${classNameLower}Service.delete(<#list table.columns as column><#if column.pk>${column.columnNameLower}</#if></#list>);
        return new ObjectRestResponse<Object>().rel(true).data(i);
    }

}
